package ipower.studentworks.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.BeanUtils;

import ipower.studentworks.dao.ClassDao;
import ipower.studentworks.domain.Class;
import ipower.studentworks.pageModel.ClassInfo;
import ipower.studentworks.service.ClassService;
import ipower.studentworks.service.UnitService;
/**
 * 班级服务实现类。
 * @author young。
 * @since 2013-11-21。
 * */
public class ClassServiceImpl extends DataServiceImpl<Class,ClassInfo> implements ClassService {
	private static final long serialVersionUID = 1L;
	private ClassDao dao;
	private UnitService unitService;

	@Override
	public void setClassDao(ClassDao classDao) {
		this.dao = classDao;
	}
	
	@Override
	public void setUnitService(UnitService unitService) {
		this.unitService = unitService;
	}

	@Override
	protected List<Class> find(ClassInfo info) {
		String hql = "from Class c where 1 = 1 ";
		Map<String, Object> parameters = new HashMap<>();
		hql = this.addWhere(info, hql, parameters);
		if(info.getSort() != null && !info.getSort().isEmpty() && info.getOrder() != null && !info.getOrder().isEmpty()){
			hql += " order by c."+ info.getSort()+ " "+ info.getOrder();
		}
		return this.dao.find(hql, parameters, info.getPage(), info.getRows());
	}

	@Override
	protected ClassInfo changeModel(Class data) {
		ClassInfo info = new ClassInfo();
		BeanUtils.copyProperties(data, info, new String[]{"unit","students"});
		if(data.getUnit() != null){
			info.setUnitID(data.getUnit().getId());
			info.setUnitName(data.getUnit().getName());
		}
		return info;
	}

	@Override
	protected Long total(ClassInfo info) {
		String hql = "select count(*) from Class c where 1 = 1 ";
		Map<String, Object> parameters = new HashMap<>();
		hql = this.addWhere(info, hql, parameters);
		return this.dao.count(hql, parameters);
	}

	@Override
	protected String addWhere(ClassInfo info, String hql, Map<String, Object> parameters) {
		if(info.getName() != null && !info.getName().isEmpty()){
			hql += " and (c.name like :name or c.code like :name or c.unit.name like :name)";
			parameters.put("name", "%"+ info.getName()+"%");
		}
		return hql;
	}
	
	@Override
	public ClassInfo update(ClassInfo info) {
		if(info != null){
			boolean isAdd = false;
			Class data = (info.getId() == null || info.getId().isEmpty()) ? null : this.dao.load(Class.class, info.getId());
			if(data == null){
				info.setId(UUID.randomUUID().toString());
				info.setCreate(new Date());
				info.setModify(null);
				
				isAdd = true;
				data = new Class();
			}else {
				info.setCreate(data.getCreate());
				info.setModify(new Date());
			}
			BeanUtils.copyProperties(info, data);
			if((data.getUnit() == null) || (!info.getUnitID().equalsIgnoreCase(data.getUnit().getId()))){
				data.setUnit(this.unitService.loadUnit(info.getUnitID()));
			}
			if(isAdd) this.dao.save(data);
		}
		return info;
	}
	
	@Override
	public void delete(String[] ids) {
		if(ids == null || ids.length == 0) return;
		for(String id : ids){
			if(id == null || id.isEmpty()) continue;
			Class data = this.dao.load(Class.class, id);
			if(data != null){
				this.dao.delete(data);
			}
		}
	}
}